module.exports.request = (params) => {
    const needLoading = params.hasOwnProperty('needLoading') ? params.needLoading : false

    let loading = true
    if (needLoading && loading) {
        tt.showLoading({
            title: '加载中',
            mask: true
        })
    }
    const baseUrl = "https://yuegui.geiqiutiyu.com/api"

    // 除了登录接口外，都需要token
    const token = params.url !== '/user/mobilelogin' || params.url !== '/column/list' ? tt.getStorageSync('userInfo')
        .token : ''
    return new Promise((resolve, reject) => {
        tt.request({
            url: baseUrl + (params.url || ''),
            method: params.method || 'POST',
            timeout: 60000,
            sslVerify: true, // 本地真机调试要改成false
            header: {
                'content-type': (params.header && params.header['content-type']) ||
                    'application/json',
                ...params.header,
                token: token
            },
            data: params.data || {},

            success: (res) => {
                console.log(res,1111)
                  if (res.data.code === 401) {
                    tt.removeStorageSync('userInfo')
                    tt.showToast({
                      title:res.data.msg,
                      icon:'none'
                    })
                    // tt.navigateTo({
                    //   url: '/pages/loginPage/index'
                    // })
                    return
                  }
                resolve(res.data)
            },
            fail: (err) => {
                console.log('接口请求失败：', err)
                reject(err)
            },
            complete: () => {
                if (needLoading && loading) {
                    tt.hideLoading()
                }
                loading = false
            }
        })
    })
}

// export default request